from django.http import JsonResponse
from Web.api.tasks import MedusaScan
from ClassCongregation import UserTable,UserScansWebsiteTable
import time
import json

#接收数据样式
'''
{
	"url": "www.ascotbe.com",
	"token": "1",
	"threads": 200,
	"module": "all",
	"header": "None"


}
'''

def register(request):
    if request.method == "POST":
        try:
            ReceiveData=json.loads(request.body)
            WebScanUrl=ReceiveData["url"]
            WebScanUserToken= ReceiveData["token"]
            WebScanModule = ReceiveData["module"]
            WebScanThreads = ReceiveData["threads"]
            WebScanAgentHeader = ReceiveData["header"]
            if type(WebScanThreads)==int:#判断类型
                if WebScanUserToken!=None:
                    result=UserTable().CheckUserToken(WebScanUserToken)
                    if result:
                        UserScansWebsiteTable().Write(WebScanUserToken,WebScanUrl,str(int(time.time())),WebScanModule)#写入用户关系表
                        MedusaScan.delay(WebScanUrl,WebScanUserToken,WebScanModule,WebScanThreads,WebScanAgentHeader)#调用扫描处理函数
                        return JsonResponse({'message': '任务下发成功', 'code': 200, })
                    else:
                        return JsonResponse({'message': '用户不存在~', 'code': 404, })
            else:
                return JsonResponse({'message': '类型错误！', 'code': 500, })

        except:
            return JsonResponse({'message': '请求不合法！', 'code': 500, })
    else:
        return JsonResponse({'message': '请使用Get请求', 'code': 500, })

